<!--电子围栏页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">围栏管理</span>
    <el-button type="primary"
               style="float:right;margin-top:13px;"
               @click="router.push('/geofenceMap')">新建围栏</el-button>
  </div>
  <!-- 围栏表格数据 -->
  <el-card style="margin:20px;">
    <el-table stripe :data="geoTableData">
      <el-table-column label="编号" prop="id" type="index" align="center" width="100"></el-table-column>
      <el-table-column label="围栏名称" prop="name" align="center"></el-table-column>
      <el-table-column label="围栏坐标点" prop="position" align="center" show-overflow-tooltip="true" width="500"></el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
      <el-table-column label="围栏状态" align="center">
        <template #default="scope">
          <el-switch inactive-value="0" active-value="1"
                     v-model="scope.row.status" @change="changeStatus(scope.row.id, scope.row.status)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="loadVehicle(scope.row.id)">管理车辆</el-button>
          <el-button type="primary" link @click="deleteGeo(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 已绑定车辆弹窗 -->
  <el-dialog title="已绑定车辆"
             v-model="bindVehicleDialogVisible"
             style="padding:40px;">
    <el-button type="primary" style="margin:10px 0;" @click="showUnbindVehicle">添加绑定车辆</el-button>
    <el-table :data="bindVehicleArr">
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center"></el-table-column>
      <el-table-column
          label="操作"
          fixed="right"
          width="200"
          align="center">
        <template #default="scope">
          <el-button link type="primary" @click="updateBindStatus(scope.row.id, 0)">移除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
  <!-- 添加可绑定车辆列表弹窗 -->
  <el-dialog title="可绑定车辆列表" v-model="AddBindVehicleDialogVisible">
    <el-table :data="unbindVehicles">
      <el-table-column label="编号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center"></el-table-column>
      <el-table-column prop="status" label="车辆状态" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="updateBindStatus(scope.row.id, 1)">绑定</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>
<script setup>
import {onMounted, ref} from "vue";
import {ElMessage} from "element-plus";
import axios from "axios";
import qs from "qs";
import router  from "@/router";
//控制"已绑定车辆弹窗"是否显示
const bindVehicleDialogVisible = ref(false);
//控制"可绑定车辆列表"是否显示
const AddBindVehicleDialogVisible = ref(false);
const geoSearchForm = ref({
  name: "",
  status: ""
})
const geoTableData = ref([])
function loadGeo() {
  let paramData=qs.stringify(geoSearchForm.value);
  console.log("paramData="+paramData);
  //发送异步请求获取数据
  axios.get(`${BASE_URL}/v1/geofence/select?${paramData}`)
      .then(res => {
        console.log("res",res);
        if (res.data.code === 2000) {
          geoTableData.value = res.data.data;
        } else {
          ElMessage.error(res.data.msg);
        }
  })
}
onMounted(() => {
  console.log("onMounted()")
  debugger
  loadGeo();
})

function resetSearch() {
  geoSearchForm.value = {
    name: "",
    status: ""
  }
  loadGeo();
}
//=======管理车辆===
const bindVehicleArr = ref([
])
let geoId = "";
function loadVehicle(geofenceId) {
  //将传过来的当前围栏的id赋值给全局变量
  geoId = geofenceId;
  //发送异步请求获取数据
  axios.get(`${BASE_URL}/v1/vehicle/select?geofenceId=${geofenceId}`)
      .then(res => {
        if (res.data.code === 2000) {
          bindVehicleArr.value = res.data.data;
          bindVehicleDialogVisible.value = true;
        } else {
          ElMessage.error(res.data.msg);
        }
      })
}
//=====解绑车辆======
 function updateBindStatus(id,bindStatus){
  let url=`${BASE_URL}/v1/vehicle/bind/${id}/${bindStatus}/${geoId}`;
  console.log("url="+url);
  axios.put(url)
      .then(res => {
        if (res.data.code === 2000) {
          ElMessage.success("更新成功");
          loadVehicle(geoId);
        } else {
          ElMessage.error(res.data.msg);
        }
  })
 }
const unbindVehicles=ref([]);
function showUnbindVehicle(){
  axios.get(`${BASE_URL}/v1/vehicle/select?geofenceBindStatus=0`)
      .then(res => {
        if (res.data.code === 2000) {
          unbindVehicles.value=res.data.data;
          AddBindVehicleDialogVisible.value = true;
        } else {
          ElMessage.error(res.data.msg);
        }
      })
}
//=========修改围栏状态===========
const changeStatus=(id,status)=>{
  let url=`${BASE_URL}/v1/geofence/save?id=${id}&status=${status}`;
  axios.post(url)
      .then(res => {
        if (res.data.code === 2000) {
          ElMessage.success("更新成功");
        } else {
          ElMessage.error(res.data.msg);
        }
      })
      .catch((error)=>{
        ElMessage.error("网络错误，稍稍重试");
   })
}
//==== 删除围栏============
const deleteGeo=(id)=>{
  if(confirm("确认删除该围栏吗?")){
    axios.delete(`${BASE_URL}/v1/geofence/delete/${id}`)
        .then(res => {
          if (res.data.code === 2000) {
            ElMessage.success("删除成功");
            loadGeo();
          } else {
            ElMessage.error(res.data.msg);
          }
        })
  }
}
</script>
<style scoped>
</style>